$(function (){
    let app = new Vue({
        el: "#app",
        data: {
            sysTitle: "固定资源管理系统",
            userID: "", //用户ID
            userName: "", //用户姓名
            userImageSrc: "", //用户头像
            userClass: "",  //用户类型
            userDepartment: "", //用户类型
            backImageSrc: "images/common/back.jpg",
            subList1: [
                {href:"properties.html", li: " 资源管理"},
                {href:"departments.html", li: " 部门管理"},
                {href:"employees.html", li: " 员工管理"}
            ],
            subList2: [
                {href:"infos.html", li: " 个人信息"},
                {href:"passwords.html", li: " 密码修改"},
                {href:"util/login.html", li: " 注销"}
            ],
            departments: [],
            employees: [],
            deEmployees: [],
            selectDepartment: "",
            dataList: [],//当前页要展示的列表数据
            empNumber: 0,
            dialogFormVisible: false,//添加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            formDataAdd: {},//表单数据
            formDataEdit: {},
            formEmp: {},
            rules: {//校验规则
                id: [{ required: true, message: '部门编号为必填项', trigger: 'blur' }],
                name: [{ required: true, message: '部门名称为必填项', trigger: 'blur' }],
                info: [{ required: true, message: '部门简介为必填项', trigger: 'blur' }],
                time: [{ required: true, message: '部门注册时间为必填项', trigger: 'blur' }],
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:7,//每页显示的记录数
                total:0,//总记录数
                description: "",
            }
        },
        created(){
            this.getIdAndName();
        },
        methods: {
            getIdAndName(){
                if(sessionStorage.getItem("userID") === null)
                {
                    location.href = "util/login.html";
                }else {
                    this.userID = sessionStorage.getItem("userID").toString();
                    this.userName = sessionStorage.getItem("userName").toString();
                    this.userImageSrc = "../" + sessionStorage.getItem("userImageSrc").toString();
                    this.userClass = sessionStorage.getItem("userClass").toString();
                    this.userDepartment = sessionStorage.getItem("userDepartment").toString();

                    this.getDataSource();
                    //显示部门数据
                    this.getAll();
                }
            },
            //分页查询
            getAll() {
                //组织参数，拼接url请求地址
                // console.log(this.pagination.type);
                param = "?info="+this.pagination.description;
                param +="&id="+this.selectDepartment;
                //发送异步请求
                axios.get("http://localhost:16/departments/"+this.pagination.currentPage+"/"+this.pagination.pageSize+param).then((res)=>{
                    this.pagination.pageSize = res.data.data.size;
                    this.pagination.currentPage = res.data.data.current;
                    this.pagination.total = res.data.data.total;
                    this.dataList = res.data.data.records;
                    for(var i = 0 ; i<this.dataList.length; i++)
                    {
                        this.dataList[i].time = this.dataList[i].time.toString().slice(0, 10);
                    }
                });
            },


            //切换页码
            handleCurrentChange(currentPage) {
                //修改页码值为当前选中的页码值
                this.pagination.currentPage = currentPage;
                //执行查询
                this.getAll();
            },

            getDataSource() {
                //为部门select绑定数据源
                axios.get("http://localhost:16/departments").then((res)=>{
                    this.departments = res.data.data;
                });

                //为部门主管select绑定数据源
                axios.get("http://localhost:16/employees").then((res)=>{
                    this.employees = res.data.data;
                });
            },

            //获取该部门所有员工
            getDepartmentEmp(id) {
                this.getDataSource();
                //为部门主管select绑定数据源
                axios.get("http://localhost:16/employees/"+"department/EmpQuery/"+id).then((res)=>{
                    console.log(res.data.data);
                    if(res.data.data.length !== 0)
                        this.deEmployees = res.data.data;
                    else this.deEmployees = this.employees;
                });
            },

            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
                this.getDataSource();
            },

            //重置表单
            resetForm() {
                this.formDataAdd = {};
            },


            //添加
            handleAdd () {
                axios.get("http://localhost:16/departments/"+this.formDataAdd.id).then((res)=>{
                    if(res.data.flag){
                        this.$message.error("系统中已存在该部门的编号！");
                    }else {
                        this.formDataAdd.number = 0;
                        axios.post("http://localhost:16/departments", this.formDataAdd).then((res)=>{
                            if(res.data.flag){
                                //关闭弹窗
                                this.dialogFormVisible = false;
                                this.$message.success(res.data.msg);
                            }else
                                this.$message.error(res.data.msg);
                        }).finally(()=>{
                            //重新加载数据
                            this.getAll();
                        });
                    }
                });
            },

            //取消
            cancel(){
                this.dialogFormVisible = false;
                this.dialogFormVisible4Edit = false;
                this.$message.info("操作已取消");
            },

            // 删除
            handleDelete(row) {
                this.$confirm("是否要删除"+row.name+"部门的信息", "删除提示", {type:"info"}).then(()=>{
                    axios.delete("http://localhost:16/departments/"+row.id).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.msg);
                            this.getDataSource();
                        }else
                            this.$message.error(res.data.msg);
                    }).finally(()=>{
                        //重新加载数据
                        this.getAll();
                    });
                }).catch(()=>{
                    this.$message.info("取消操作");
                });

            },

            //弹出编辑窗口
            handleUpdate(row) {
                this.getDepartmentEmp(row.id);
                axios.get("http://localhost:16/departments/"+row.id).then((res)=>{
                    if(res.data.flag && res.data.data != null)
                    {
                        axios.get("http://localhost:16/employees/"+row.eid).then((res)=>{
                            if(res.data.flag)
                            {
                                this.formEmp = res.data.data;
                            }
                        });
                        this.dialogFormVisible4Edit = true;
                        this.formDataEdit = res.data.data;
                    }else {
                        this.$message.error("数据同步失败，自动刷新");
                    }
                }).finally(()=>{
                    this.getAll();
                });
            },

            //修改
            handleEdit() {
                if(this.formDataEdit.name === "" || this.formDataEdit.info === ""){
                    this.$message.error("信息填写不全,请补充！");
                    return;
                }

                var departEmp;
                axios.get("http://localhost:16/departments").then((res)=>{
                    departEmp = res.data.data;
                });

                axios.put("http://localhost:16/departments", this.formDataEdit).then((res)=>{
                    if(res.data.flag)
                    {
                        this.dialogFormVisible4Edit = false;
                        this.$message.success(res.data.msg);
                    }else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(()=>{
                    this.getAll();
                });
            },


        }
    });
    var left_nav = $(".left-nav");
    var left_lis = left_nav.children("li");
    var left_sub_nav = left_lis[0].children[1];
    $(left_sub_nav).children("li").eq(1).children("a").addClass("current");
   /* lis.addClass("current");*/
});